package com.woniuxy.cloud.util;

import java.util.Random;

/**
 * 生成特殊编号
 * @author 周川雄
 * @date 2022/1/3 20:38
 */
public class CreateNumberUtil {

    /**
     * 编号设置（前缀-时间后四位-顺序号三位）
     * @param prefix    前缀  WONIUXY
     * @param previousNumber    前一个大类编号，用于取出顺序号 WONIUXY-3046-7862
     * @return  编号  WONIUXY-5206-7863
     */
    //
    public static String getNumber(String prefix, String previousNumber) {

        String time = String.valueOf(System.currentTimeMillis());
        time = time.substring(time.length()-4);
        String s;

        //第一次添加
        if(previousNumber == null) {
            s = "001";
        }else {
            //获取顺序号三位，获取异常则随机四位数
            try {
                s = Id2NumberUtil.getNumber(Integer.valueOf(previousNumber.substring(previousNumber.lastIndexOf("-")+1)) + 1);
            } catch (Exception e) {     //转换异常
                Random random = new Random();
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < 4; i++) {
                    stringBuffer.append(random.nextInt(9));
                }
                s = stringBuffer.toString();
            }
        }
        String number = prefix + "-" + time + "-" + s;
        return number;

    }
}
